<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <link rel="stylesheet" href="plugin/element-ui/index.css">
    <link rel="stylesheet" href="style/login.css" />
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

<div id="main">
    <div id="app">

        <div class="login_box">
            <img src="img/icon.jpg" />
<!--            <div style="width: 100%">-->
<!--                <form th:action="@{/login}" method="post">-->
<!--                    <input type="text" name="username" placeholder="账号"><br>-->
<!--                    <input type="password" name="password" placeholder="密码"><br>-->
<!--                    <input style="width: 7rem;background-color: #fffe26;width: 7rem;border: 1px solid #b2b13d;" type="submit" value="登录">-->
<!--                </form>-->
<!--            </div>-->

            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                     class="demo-ruleForm">
                <h3>登录</h3>
                <el-form-item label="账号" prop="username">
                    <el-input name="username" type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input name="password" type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

    </div>
</div>
    <script src="plugin/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="plugin/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="plugin/axios/axios.min.js"></script>
    <script src="js/login.js"></script>
    <script>
        var app = new Vue({
            el: "#app",
            data() {
                var checkUsername = (rule, value, callback) => {
                    if (!value) {
                        return callback(new Error('账号不能为空'));
                    }
                    setTimeout(() => {
                        if (value.length < 3) {
                            callback(new Error('请输入长度为3-10位的账号'));
                        } else {
                            if (value.length > 10) {
                                callback(new Error('请输入长度为3-10位的账号'));
                            } else {
                                callback();
                            }
                        }
                    }, 100);
                };
                var validatePass = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入密码'));
                    } else {
                        callback();
                    }
                };
                return {
                    ruleForm: {
                        username: 'admin',
                        password: '123456'
                    },
                    rules: {
                        username: [
                            { validator: checkUsername, trigger: 'blur' }
                        ],
                        password: [
                            { validator: validatePass, trigger: 'blur' }
                        ]
                    }
                };
            },
            methods: {
                async submitForm(formName) {
                    this.$refs[formName].validate(async (valid) => {
                        if (valid) {
                            let res = await login(this.ruleForm)
                            console.log(res)

                            if(String(res.code)=='1'){
                                window.localStorage.setItem("userInfo",JSON.stringify(res.data))
                                window.location.href="page/index.html"
                            }else{
                                this.$message.error(res.msg)
                            }
                        } else {
                            console.log('登录错误');
                            return false;
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                }
            }
        })
    </script>
</body>

</html>